//
//  KKBLESend.m
//
//  Created by zorro on 2017/11/13.
//  Copyright © 2017年 zorro. All rights reserved.
//

#import "KKBLESend.h"
#import "KKBLEService.h"
@implementation KKBLESend

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(BecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(Becomeback) name:UIApplicationDidEnterBackgroundNotification object:nil];
    }
    return self;
}

- (void)BecomeActive
{
    
}

- (void)Becomeback
{
    
}

// 发送数据
- (void)sendBytes:(UInt8 *)val length:(NSInteger)length block:(KKBLEAcceptBlock)block {
    NSData *data = [[NSData alloc] initWithBytes:val length:length];
    
    LogWrite(nil, ([NSString stringWithFormat:@"%@", data]), nil);
    [self sendData:data block:block];
}

- (void)sendData:(NSData *)data block:(KKBLEAcceptBlock)block {
    usleep(50000);
    KK_BLEAccept.acceptStatus = block;
    KK_BLEAccept.acceptType   = KKBLEAcceptTypeUnKnown;
    [KK_BLEService senderDataToPeripheral:data];
}

- (void)resetSendCMD {
    [self resetAllupdateData];
    _isSend = NO;
}

// 连接上时发送一大串连续的指令
- (void)sendCMD {
    NSLog(@"sendCMD>>>>>%@  %@", [KK_BLELastDevice getObjectValue], KK_BLEConnect.model.bleUUID);
    if (KK_BLEConnect.model.bleUUID.length > 0) {
        [KK_BLELastDevice setObjectValue:KK_BLEConnect.model.bleUUID];
        [KK_BLELastDeviceMac setObjectValue:KK_BLEConnect.model.bleMAC];
        [SN_USER updateToDB];
    }
    [self resetSendCMD];
    _isSend = NO;
    LogWrite(@"重连刷新", nil, nil);
    if ([KK_BLEModel.chip containsString:@"nrf"]) {
        _bleWaitTime = 0.25;
    } else {
        _bleWaitTime = 1.0;
    }
    [self performSelector:@selector(setting) withObject:nil afterDelay:1.0];
    [self performSelector:@selector(settingEnd) withObject:nil afterDelay:60.0];
    //存在拍照/心率忘记关的时候 连接上先发送停止指令
    [self sendControlTakePhotoState:NO WithUpdateBlock:^(id object, KKBLEAcceptType type) {
    }];
    [self sendHeartStatus:NO type:1 WithUpdate:^(id object, KKBLEAcceptType type) {
    }];
    [self sendHeartStatus:NO type:2 WithUpdate:^(id object, KKBLEAcceptType type) {
    }];
    [self sendHeartStatus:NO type:3 WithUpdate:^(id object, KKBLEAcceptType type) {
    }];
}

//同步时间
- (void)setting {
    if (_isSend) {
        //        SHOWMBProgressHUD(@"正在同步请稍后...", nil, nil, NO, 2.0);
        return;
    }
    _isSend = YES;
    [self sendSetTimeforDeviceWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeTime) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting1) object:nil];
            [self performSelector:@selector(setting1) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

//同步实时步数
- (void)setting1 {
    [self sendGetRealStepWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeRealtimeSport) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting7) object:nil];
            [self performSelector:@selector(setting2) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

// 同步Mac地址
- (void)setting2 {
    [self sendGetDeviceMacAddress:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDevideMAC) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting2) object:nil];
            [self performSelector:@selector(setting3) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

//获取设备信息
- (void)setting3 {
    [self sendGetDeviceBasicInfoWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDevideInfo) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting3) object:nil];
            [self performSelector:@selector(setting4) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

// 设置用户信息
- (void)setting4 {
    [self sendUserInfoSettingWithBlock:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeUserInfo) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting4) object:nil];
            [self performSelector:@selector(setting5) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

// 设置提醒
- (void)setting5 {
    [self sendSetLowElecWithUpdateBlock:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeOther) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting5) object:nil];
            [self performSelector:@selector(setting6) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

// 推送提醒
- (void)setting6 {
    [self sendSetPushNoticeWithUpdateBlock:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeNotify) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting6) object:nil];
            [self performSelector:@selector(setting7) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

//获取步数大数据
- (void)setting7 {
    _isSend = YES;
    [self sendBigStepWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDataSport) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting9) object:nil];
            [self performSelector:@selector(setting8) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

//获取睡眠大数据
- (void)setting8 {
    [self sendBigSleepWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDataSleep) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting9) object:nil];
            [self performSelector:@selector(setting9) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

//获取心率大数据
- (void)setting9 {
    //    NSLog(@"SN_USER.heartFunction>>>>%d",SN_USER.heartFunction);
    //    NSArray *tw64Array = [[NSArray alloc]initWithObjects:@"M2", @"F115", @"115", @"X10", @"X8", @"X64", @"TW64", nil];
    //    [tw64Array containsObject:KK_BLEModel.bleName]；
    //
    //    if (!SN_USER.heartFunction) {
    //        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingDrink) object:nil];
    //        [self performSelector:@selector(settingDrink) withObject:nil afterDelay:_bleWaitTime];
    //    } else {
    //        [self sendBigheartWithUpdate:^(id object, KKBLEAcceptType type) {
    //            if (type == KKBLEAcceptTypeDataHeart) {
    //                [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingDrink) object:nil];
    //                [self performSelector:@selector(settingDrink) withObject:nil afterDelay:_bleWaitTime];
    //            }
    //        }];
    //    }
    [self sendBigheartWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDataHeart) {
        }
    }];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingDrink) object:nil];
    [self performSelector:@selector(settingDrink) withObject:nil afterDelay:5.0];
}

- (void)settingDrink {
    [SN_EveryDayModelHelp startDrinkWater];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingSedentaryRemind) object:nil];
    [self performSelector:@selector(settingSedentaryRemind) withObject:nil afterDelay:_bleWaitTime];
}

- (void)settingSedentaryRemind {
    [SN_EveryDayModelHelp startsedentary];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingAlarm) object:nil];
    [self performSelector:@selector(settingAlarm) withObject:nil afterDelay:_bleWaitTime];
}

- (void)settingAlarm {
    [SN_EveryDayModelHelp startSetAlarmClock];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingCalendar) object:nil];
    [self performSelector:@selector(settingCalendar) withObject:nil afterDelay:5.0];
}

- (void)settingCalendar {
    [SN_EveryDayModelHelp startSetCalendar];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingIgnoreAll) object:nil];
    [self performSelector:@selector(settingIgnoreAll) withObject:nil afterDelay:5.0];
}

- (void)settingIgnoreAll {
    [self sendNotDisturbWithBlock:^(id object, KKBLEAcceptType type) {
    }];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingEnd) object:nil];
    [self performSelector:@selector(settingEnd) withObject:nil afterDelay:_bleWaitTime];
}

// 设置用户信息
- (void)setting10 {
    [self sendUserInfoSettingWithBlock:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeUserInfo) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting4) object:nil];
            [self performSelector:@selector(settingEnd) withObject:nil afterDelay:_bleWaitTime];
        }
    }];
}

- (void)settingEnd {
    NSLog(@"蓝牙同步设置结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    SN_USER.lastUpdateTime = [[NSDate date]dateToString];
    [SN_USER updateToDB];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingEnd) object:nil];
    _isSend = NO;
}

// 升级
- (void)sendUpdateFirmwareWithUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x00, 0x02};
    
    LogWrite(@"升级", nil, nil);
    [self sendBytes:val length:3 block:block];
}

/*******************************X9协议**************************/
//设置时间
- (void)sendSetTimeforDeviceWithUpdate:(KKBLEAcceptBlock)block {
    NSDate *date      = [NSDate date];
    int    i          = date.year;
    int    yearHeight = 0;
    int    yearLow    = 0;
    
    yearHeight = i / 256;
    yearLow    = i - yearHeight *256;
    UInt8 val[10] = {0x05, 0x02, 0x01, yearHeight, yearLow, date.month, date.day, date.hour, date.minute, date.second};
    LogWrite(@"设置时间", nil, nil);
    [self sendBytes:val length:10 block:block];
}

//获取设备mac地址
- (void)sendGetDeviceMacAddress:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x01, 0x01};
    
    LogWrite(@"获取mac地址", nil, nil);
    [self sendBytes:val length:3 block:block];
}

//获取设备基本信息
- (void)sendGetDeviceBasicInfoWithUpdate:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x01, 0x02};
    
    LogWrite(@"获取设备基本信息", nil, nil);
    [self sendBytes:val length:3 block:block];
}

// 系统配对
- (void)sendUserPairWithBlock:(KKBLEAcceptBlock)block
{
    int ios = 0x00;  // 00 表示ios  //01 表示安卓 // 02 表示已经配对了
    int step1 = 0;
    int step2 = 0;
    int step3 = 0;
    int step4 = 0;
    step4 = SN_USER.target_step %256;
    step3 = SN_USER.target_step /256;
    step2 = SN_USER.target_step /256/256;
    step1 = SN_USER.target_step /256/256/256;
    NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    NSRange  containsA             = [formatStringForHours rangeOfString:@"a"];
    BOOL     hasAMPM               = containsA.location != NSNotFound;
    //    SN_USER.is24Time TURE为12小时制，否则为24小时制
    UInt8 val[17] = {0x05, 0x02, 0x02, SN_USER.gender, SN_USER.age, SN_USER.height,
        (int)SN_USER.weight, SN_USER.lanuage, hasAMPM, SN_USER.isMetricSystem, ios, SN_USER.hand, SN_USER.temperatureSet, step1, step2, step3, step4};
    LogWrite(@"设置用户信息", nil, nil);
    [self sendBytes:val length:17 block:block];
}

// 用户信息
- (void)sendUserInfoSettingWithBlock:(KKBLEAcceptBlock)block {
    int ios = 0x00;  // 00 表示ios  //01 表示安卓 // 02 表示已经配对了
    
    if (KK_BLEConnect.isFoundPair) {
        ios = 0x02;   // 00 表示ios  //01 表示安卓
    } else {
        ios = 0x00;   // 00 表示ios  //01 表示安卓
    }
    int step1 = 0;
    int step2 = 0;
    int step3 = 0;
    int step4 = 0;
    step4 = SN_USER.target_step %256;
    step3 = SN_USER.target_step /256;
    step2 = SN_USER.target_step /256/256;
    step1 = SN_USER.target_step /256/256/256;
    NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    NSRange  containsA             = [formatStringForHours rangeOfString:@"a"];
    BOOL     hasAMPM               = containsA.location != NSNotFound;
    //    SN_USER.is24Time TURE为12小时制，否则为24小时制
    UInt8 val[17] = {0x05, 0x02, 0x02, SN_USER.gender, SN_USER.age, SN_USER.height,
        (int)SN_USER.weight, SN_USER.lanuage, hasAMPM, SN_USER.isMetricSystem, ios, SN_USER.hand, SN_USER.temperatureSet, step1, step2, step3, step4};
    LogWrite(@"设置用户信息", nil, nil);
    [self sendBytes:val length:17 block:block];
}

//免打扰
- (void)sendNotDisturbWithBlock:(KKBLEAcceptBlock)block {
    UInt8 val[8] = {0x05, 0x02, 0x0B, SN_USER.isOpen, SN_USER.startH, SN_USER.startM, SN_USER.endH, SN_USER.endM};
    
    LogWrite(@"免打扰设置", nil, nil);
    NSData *data = [[NSData alloc] initWithBytes:val length:9];
    [KK_BLEService senderDataToPeripheral:data];
}

//实时步数
- (void)sendGetRealStepWithUpdate:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x07, 0x01};
    
    LogWrite(@"获取实时步数", nil, nil);
    [self sendBytes:val length:3 block:block];
}

//记步大数据
- (void)sendBigStepWithUpdate:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x07, 0x03};
    
    LogWrite(@"获取计步大数据", nil, nil);
    [self sendBytes:val length:3 block:block];
}

//睡眠大数据
- (void)sendBigSleepWithUpdate:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x07, 0x04};
    
    LogWrite(@"获取睡眠大数据", nil, nil);
    [self sendBytes:val length:3 block:block];
}

//心率大数据 2017 10 26
- (void)sendBigheartWithUpdate:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x07, 0x07};
    
    LogWrite(@"获取心率大数据", nil, nil);
    [self sendBytes:val length:3 block:block];
}

// 拍照控制
- (void)sendControlTakePhotoState:(BOOL)type WithUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 val[4] = {0x05, 0x05, 0x02, type};
    
    LogWrite(@"拍照", nil, nil);
    [self sendBytes:val length:4 block:block];
}

// 找手环
- (void)sendSetFindBle:(BOOL)Find withUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 val[4] = {0};
    
    val[0] = 0x05;
    val[1] = 0x05;
    val[2] = 0x03;
    val[3] = Find;
    LogWrite(@"找手环", nil, nil);
    NSData *data = [[NSData alloc] initWithBytes:val length:4];
    [KK_BLEService senderDataToPeripheral:data];
}

// 心率开关 血氧 血压开关
- (void)sendHeartStatus:(BOOL)status type:(NSInteger)type WithUpdate:(KKBLEAcceptBlock)block {
    if (type == 1) {
        UInt8 val[4] = {0x05, 0x05, 0x01, status};
        [self sendBytes:val length:4 block:block];
        LogWrite(@"手动心率", nil, nil);
    }
    if (type == 2) {
        UInt8 val[4] = {0x05, 0x05, 0x04, status};
        [self sendBytes:val length:4 block:block];
        LogWrite(@"手动血氧", nil, nil);
    }
    if (type == 3) {
        UInt8 val[4] = {0x05, 0x05, 0x05, status};
        [self sendBytes:val length:4 block:block];
        LogWrite(@"手动血压", nil, nil);
    }
}

//天气
- (void)sendTemperatureParamWithBlock:(KKBLEAcceptBlock)block {
    NSArray *sunArr      = @[@"100"];
    NSArray *cloudArr    = @[@"101", @"102", @"103", @"101", @"101", @"201", @"202", @"203", @"204"];
    NSArray *snowArr     = @[@"400", @"401", @"402", @"403", @"404", @"405", @"406", @"407", ];
    NSArray *overcastArr = @[@"104", @"200", @"205", @"206", @"207", @"208", @"210", @"500", @"501", @"502", @"503", @"504", @"507", @"508"];
    NSArray *rainArr     = @[@"209", @"211", @"212", @"213", @"300", @"301", @"302", @"304", @"305", @"306", @"307", @"308", @"309", @"310", @"311", @"312", @"313"];
    NSArray *nonArr      = @[@"900", @"901", @"902"];
    int     type         = 0x00;
    
    if ([sunArr containsObject:KK_WeatherModel.weatherCode]) {
        type = 0x01;
    } else if ([cloudArr containsObject:KK_WeatherModel.weatherCode]) {
        type = 0x02;
    } else if ([snowArr containsObject:KK_WeatherModel.weatherCode]) {
        type = 0x18;
    } else if ([overcastArr containsObject:KK_WeatherModel.weatherCode]) {
        type = 0x03;
    } else if ([rainArr containsObject:KK_WeatherModel.weatherCode]) {
        type = 0x07;
    } else if ([nonArr containsObject:KK_WeatherModel.weatherCode]) {
        type = 0x00;
    }
    int   maxTem     = [KK_WeatherModel.maxTem intValue];
    int   minTem     = [KK_WeatherModel.minTem intValue];
    int   currentTem = [KK_WeatherModel.tem intValue];
    UInt8 val[11]    = {0x05, 0x02, 0x0E, type, 0x00, 0x00, currentTem, 0x00, maxTem, 0x00, minTem};
    //[self sendBytes:val length:12 block:block];
    NSData *data = [[NSData alloc] initWithBytes:val length:12];
    [KK_BLEService senderDataToPeripheral:data];
    LogWrite(@"天气", ([NSString stringWithFormat:@"%@", data]), nil);
}

// 发送低电 抬腕 防丢 心率检测 翻腕切屏
- (void)sendSetLowElecWithUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 val[8] = {0x05, 0x02, 0x0A, SN_USER.lowElecNotice, SN_USER.liftNotice, SN_USER.lostNotice, SN_USER.heartAutoCheck, SN_USER.changeScreen};
    
    LogWrite(@"低电 抬腕 防丢 心率检测 翻腕切屏", nil, nil);
    [self sendBytes:val length:8 block:block];
}

// 消息推送 电话 短信
- (void)sendSetPushNoticeWithUpdateBlock:(KKBLEAcceptBlock)block {
    int MessageNotice = 0;
    
    if (SN_USER.ancsNotice) {
        if (KK_BLEModel.bleCustomerID == 0) {
            MessageNotice = 255; // 旧版为 bit 0 ~ bit 5 对应的开关
        } else {
            MessageNotice = 1;   // 新版只有一个总开关1
        }
    } else {
        MessageNotice = 0;
    }
    UInt8 val[6] = {0x05, 0x02, 0x09, SN_USER.phoneNotice, SN_USER.smsNotice, MessageNotice};
    LogWrite(@"消息推送 电话 短信", nil, nil);
    [self sendBytes:val length:6 block:block];
}

- (void)sendDrinkWithUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 week = 0;
    
    week = bitInsertInt(week, 0, SN_USER.drinkSUN);
    week = bitInsertInt(week, 1, SN_USER.drinkMON);
    week = bitInsertInt(week, 2, SN_USER.drinkTUE);
    week = bitInsertInt(week, 3, SN_USER.drinkWED);
    week = bitInsertInt(week, 4, SN_USER.drinkTHU);
    week = bitInsertInt(week, 5, SN_USER.drinkFRI);
    week = bitInsertInt(week, 6, SN_USER.drinkSAT);
    week = bitInsertInt(week, 7, SN_USER.drinkOnce);
    UInt8 val[18] = {0x05, 0x02, 0x06, SN_USER.drinkOpen, week,
        SN_USER.drinkStartHour1, SN_USER.drinkStartMins1, SN_USER.drinkEndHour1, SN_USER.drinkEndMins1,
        SN_USER.drinkStartHour2, SN_USER.drinkStartMins2, SN_USER.drinkEndHour2, SN_USER.drinkEndMins2,
        SN_USER.drinkStartHour3, SN_USER.drinkStartMins3, SN_USER.drinkEndHour3, SN_USER.drinkEndMins3, SN_USER.drinkinterval};
    LogWrite(@"喝水提醒", nil, nil);
    [self sendBytes:val length:18 block:block];
}

- (void)sendSedentaryRemindWithUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 week = 0;
    
    week = bitInsertInt(week, 0, SN_USER.sedentarySUN);
    week = bitInsertInt(week, 1, SN_USER.sedentaryMON);
    week = bitInsertInt(week, 2, SN_USER.sedentaryTUE);
    week = bitInsertInt(week, 3, SN_USER.sedentaryWED);
    week = bitInsertInt(week, 4, SN_USER.sedentaryTHU);
    week = bitInsertInt(week, 5, SN_USER.sedentaryFRI);
    week = bitInsertInt(week, 6, SN_USER.sedentarySAT);
    week = bitInsertInt(week, 7, SN_USER.sedentaryOnce);
    UInt8 val[18] = {0x05, 0x02, 0x07, SN_USER.sedentaryOpen, week,
        SN_USER.sedentaryStartHour1, SN_USER.sedentaryStartMins1, SN_USER.sedentaryEndHour1, SN_USER.sedentaryEndMins1,
        SN_USER.sedentaryStartHour2, SN_USER.sedentaryStartMins2, SN_USER.sedentaryEndHour2, SN_USER.sedentaryEndMins2,
        SN_USER.sedentaryStartHour3, SN_USER.sedentaryStartMins3, SN_USER.sedentaryEndHour3, SN_USER.sedentaryEndMins3, SN_USER.sedentaryinterval};
    LogWrite(@"日程提醒", nil, nil);
    [self sendBytes:val length:18 block:block];
}

// 闹钟
- (void)sendSetDeviceAlarmClock_KK:(SNAlarmModel *)model
                   withUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 week = 0;
    
    week = bitInsertInt(week, 0, model.SUN);
    week = bitInsertInt(week, 1, model.MON);
    week = bitInsertInt(week, 2, model.TUE);
    week = bitInsertInt(week, 3, model.WED);
    week = bitInsertInt(week, 4, model.THU);
    week = bitInsertInt(week, 5, model.FRI);
    week = bitInsertInt(week, 6, model.SAT);
    week = bitInsertInt(week, 7, model.Once);
    UInt8 val[9] = {0x05, 0x02, 0x04, model.orderIndex, model.open, week,
        model.hours, model.mins, model.activeType};
    LogWrite(@"闹钟", nil, nil);
    [self sendBytes:val length:9 block:block];
}

// 日常提醒
- (void)sendSetDeviceCalendarModel:(SNcalendarModel *)model
                   withUpdateBlock:(KKBLEAcceptBlock)block {
    UInt8 val[11] = {0x05, 0x02, 0x05, model.orderIndex, model.open, model.year - 2000, model.month, model.day,
        model.hours, model.mins, model.activeType};
    
    LogWrite(@"日程提醒", nil, nil);
    [self sendBytes:val length:11 block:block];
}

//设备重启
- (void)sendDeviceRebootWithUpdate:(KKBLEAcceptBlock)block {
    UInt8 val[3] = {0x05, 0x00, 0x01};
    
    LogWrite(@"设备重启", nil, nil);
    [self sendBytes:val length:3 block:block];
}

// 主页下拉 刷新 调用
- (void)updateData {
    if (_isSend) {
        SHOWMBProgressHUD(SN_Text(@"更新中..."), nil, nil, NO, 2.0);
        return;
    }
    _isSend = YES;
    LogWrite(@"手动下拉刷新", nil, nil);
    [self sendGetRealStepWithUpdate:^(id object, KKBLEAcceptType type)
     {
         if (type == KKBLEAcceptTypeRealtimeSport) {
             [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateData1) object:nil];
             [self performSelector:@selector(updateData1) withObject:self afterDelay:_bleWaitTime];
         }
     }];
}

- (void)updateData1 {
    [self sendBigStepWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDataSport) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateData2) object:nil];
            [self performSelector:@selector(updateData2) withObject:self afterDelay:_bleWaitTime];
        }
    }];
}

- (void)updateData2 {
    [self sendBigSleepWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDataSleep) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateData3) object:nil];
            [self performSelector:@selector(updateData3) withObject:self afterDelay:1.0];
        }
    }];
}

- (void)updateData3 {
    [self sendBigheartWithUpdate:^(id object, KKBLEAcceptType type) {
        if (type == KKBLEAcceptTypeDataHeart) {
        }
    }];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateDataEnd) object:nil];
    [self performSelector:@selector(updateDataEnd) withObject:self afterDelay:5.0];
}

- (void)updateDataEnd {
    SN_USER.lastUpdateTime = [[NSDate date]dateToString];
    NSLog(@"更新大数据同步结束>>>>>>>%@", [[NSDate date]dateToString]);
    [SN_USER updateToDB];
    _isSend = NO;
}

- (void)resetAllupdateData {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting1) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting2) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting3) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting4) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting5) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting6) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting7) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting8) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(setting9) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingDrink) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingSedentaryRemind) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingAlarm) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingCalendar) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(settingIgnoreAll) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateData3) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateData1) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateData2) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateDataEnd) object:nil];
}

@end

